package com.ouzexi.finalexp.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import com.ouzexi.finalexp.Constants
import com.ouzexi.finalexp.R
import com.ouzexi.finalexp.vo.Position

class PositionAdapter(context: Context?, resource: Int, objects: List<Position?>?) :
    ArrayAdapter<Position?>(
        context!!, resource, objects!!
    ) {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        val record = getItem(position)
        val viewHolder: ViewHolder
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.position_item, null)
            viewHolder = ViewHolder()
            viewHolder.positionName = convertView.findViewById(R.id.tv_position_name)
            viewHolder.positionSalary = convertView.findViewById(R.id.tv_position_salary)
            viewHolder.positionWorkType = convertView.findViewById(R.id.tv_position_work_type)
            convertView.tag = viewHolder
        } else {
            viewHolder = convertView.tag as ViewHolder
        }

        // 设置textview
        if (record!!.salaryType == Constants.SALARY_TYPE.MONTH) {
            viewHolder.positionSalary!!.text = record.salary.toString() + " / 月"
        } else {
            viewHolder.positionSalary!!.text = record.salary.toString() + " / 日"
        }
        when (record.workType) {
            Constants.WORK_TYPE.FULL_TIME -> viewHolder.positionWorkType!!.text = "全职"
            Constants.WORK_TYPE.PART_TIME -> viewHolder.positionWorkType!!.text = "兼职"
            else -> viewHolder.positionWorkType!!.text = "实习"
        }
        viewHolder.positionName!!.text = record.positionName
        return convertView!!
    }

    internal inner class ViewHolder {
        var positionName: TextView? = null
        var positionSalary: TextView? = null
        var positionWorkType: TextView? = null
    }
}